package org.jeecg.cn.edu.scau.vm.review.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.cn.edu.scau.vm.common.utils.RedisIdWorker;
import org.jeecg.cn.edu.scau.vm.review.entity.VmSchedule;
import org.jeecg.cn.edu.scau.vm.review.enums.VmScheduleEnum;
import org.jeecg.cn.edu.scau.vm.review.service.impl.IVmScheduleService;
import org.jeecg.cn.edu.scau.vm.review.vo.VmScheduleVo;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Api(tags = "用户特殊配置审核")
@RestController
@RequestMapping("/vm/vmSchedule")
@Slf4j
public class VmScheduleController {
    @Resource
    private IVmScheduleService vmGpuScheduleService;

    @Resource
    private RedisIdWorker redisIdWorker;

    /**
     * 分页列表查询
     *
     * @param vmSchedule 查询实例
     * @param pageNo     查询页码
     * @param pageSize   查询页大小
     * @param req        查询请求
     * @return 分页对象
     */
    //@AutoLog(value = "用户特殊配置审核-分页列表查询")
    @ApiOperation(value = "用户特殊配置审核-分页列表查询", notes = "用户特殊配置审核-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<VmScheduleVo>> queryPageList(VmSchedule vmSchedule,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                     HttpServletRequest req) {
        QueryWrapper<VmSchedule> queryWrapper = QueryGenerator.initQueryWrapper(vmSchedule, req.getParameterMap());
        Page<VmSchedule> page = new Page<>(pageNo, pageSize);
        IPage<VmScheduleVo> pageList = vmGpuScheduleService.pageDetail(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 分页列表查询未审核
     *
     * @param vmSchedule 查询实例
     * @param pageNo     查询页码
     * @param pageSize   查询页大小
     * @param req        查询请求
     * @return 分页对象
     */
    //@AutoLog(value = "用户特殊配置审核-分页列表查询")
    @ApiOperation(value = "用户特殊配置审核-分页列表查询未审核", notes = "用户特殊配置审核-分页列表查询未审核")
    @GetMapping(value = "/listNoReview")
    public Result<IPage<VmScheduleVo>> queryPageListNoReview(VmSchedule vmSchedule,
                                                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                             HttpServletRequest req) {
        QueryWrapper<VmSchedule> queryWrapper = QueryGenerator.initQueryWrapper(vmSchedule, req.getParameterMap());
        queryWrapper.eq("approve_status", VmScheduleEnum.PROCESSING.getCode());
        Page<VmSchedule> page = new Page<>(pageNo, pageSize);
        IPage<VmScheduleVo> pageList = vmGpuScheduleService.pageDetail(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 分页列表查询我的特批审核
     *
     * @param vmSchedule 查询实例
     * @param pageNo     查询页码
     * @param pageSize   查询页大小
     * @param req        查询请求
     * @return 分页对象
     */
    //@AutoLog(value = "用户特殊配置审核-分页列表查询")
    @ApiOperation(value = "用户特殊配置审核-分页我的列表查询", notes = "用户特殊配置审核-分页我的列表")
    @GetMapping(value = "/listMy")
    public Result<IPage<VmScheduleVo>> queryPageListMy(VmSchedule vmSchedule,
                                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                       HttpServletRequest req) {
        QueryWrapper<VmSchedule> queryWrapper = QueryGenerator.initQueryWrapper(vmSchedule, req.getParameterMap());
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        queryWrapper.eq("create_by", loginUser.getId());
        Page<VmSchedule> page = new Page<>(pageNo, pageSize);
        IPage<VmScheduleVo> pageList = vmGpuScheduleService.pageDetail(page, queryWrapper);
        return Result.OK(pageList);
    }


    /**
     * 添加特殊审核配置
     *
     * @param vmSchedule 实体
     * @return 返回是否添加成功
     */
    @AutoLog(value = "用户特殊配置审核-添加")
    @ApiOperation(value = "用户特殊配置审核-添加", notes = "用户特殊配置审核-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody VmSchedule vmSchedule) {
        //生成uuid
        vmSchedule.setScheduleUuid(redisIdWorker.nextId("VmGpuSchedule"));
        vmGpuScheduleService.save(vmSchedule);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param vmSchedule 用户特殊配置审核
     * @return 返回是否编辑成功
     */
    @AutoLog(value = "用户特殊配置审核-编辑")
    @ApiOperation(value = "用户特殊配置审核-编辑", notes = "用户特殊配置审核-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody VmSchedule vmSchedule) {
        vmGpuScheduleService.updateById(vmSchedule);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id 数据库ID
     * @return 提示语
     */
    @AutoLog(value = "用户特殊配置审核-通过id删除")
    @ApiOperation(value = "用户特殊配置审核-通过id删除", notes = "用户特殊配置审核-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        vmGpuScheduleService.removeById(id);
        return Result.OK("删除成功!");
    }


    /**
     * 基于ID,审核通过
     *
     * @param id 数据库ID
     * @return 提示语
     */
    @AutoLog(value = "用户特殊配置审核-审核通过")
    @ApiOperation(value = "用户特殊配置审核-审核通过", notes = "用户特殊配置审核-审核通过")
    @DeleteMapping(value = "/schedulePass")
    public Result<String> schedulePass(@RequestParam(name = "id", required = true) String id,
                                       @RequestParam(name = "reviewMsg", required = false) String reviewMsg) {
        vmGpuScheduleService.schedulePass(id, reviewMsg);
        return Result.OK("审批完成!");
    }

    /**
     * 审批不通过
     *
     * @param id 数据库ID
     * @return 提示语
     */
    @AutoLog(value = "用户特殊配置审核-审批不通过")
    @ApiOperation(value = "用户特殊配置审核-审批不通过", notes = "用户特殊配置审核-审批不通过")
    @DeleteMapping(value = "/scheduleReject")
    public Result<String> scheduleReject(@RequestParam(name = "id", required = true) String id,
                                         @RequestParam(name = "reviewMsg", required = false) String reviewMsg) {
        vmGpuScheduleService.scheduleReject(id, reviewMsg);
        return Result.OK("审批完成!");
    }

    /**
     * 批量删除
     *
     * @param ids id列表
     * @return 返回提示语
     */
    @AutoLog(value = "用户特殊配置审核-批量删除")
    @ApiOperation(value = "用户特殊配置审核-批量删除", notes = "用户特殊配置审核-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.vmGpuScheduleService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "用户特殊配置审核-通过id查询")
    @ApiOperation(value = "用户特殊配置审核-通过id查询", notes = "用户特殊配置审核-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<VmSchedule> queryById(@RequestParam(name = "id", required = true) String id) {
        VmSchedule vmSchedule = vmGpuScheduleService.getById(id);
        if (vmSchedule == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(vmSchedule);
    }


}
